﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace carsitem.kehu
{
    public partial class Updatecustomer : Form
    {
        public int cus;
        public Updatecustomer()
        {
            InitializeComponent();
        }

        private void ChongzhiBt_Click(object sender, EventArgs e)
        {
           
            foreach (Control con in this.Controls)//遍历控件类集合
            {
                TextBox text = con as TextBox;//获取TextBox对象
                RadioButton rdo = con as RadioButton;//获取RadioButton对象
                CheckBox che = con as CheckBox;//获取CheckBox对象
                if (text != null)//文本框清空
                {
                    text.Text = "";
                }
                if (che != null)//多选框全不选
                {
                    che.Checked = false;
                }
            }
        }

        private void OutBt_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void UpBt_Click(object sender, EventArgs e)
        {
            string customerName = this.NameBox.Text;
            string customersex = "";
            if (this.ManRbt.Checked == true)
            {
                customersex = "男";
            }
            if (this.WmanRbt.Checked == true)
            {
                customersex = "女";
            }
            string number = this.NumberBox.Text;
            string company = this.CompanyBox.Text;
            string Address = this.AddressBox.Text;

            string sql = string.Format(@"update customerTable set customerName='{0}',customersex='{1}',customernumber={2},
              cAddress='{3}',workAddress='{4}'where customerID={5}", customerName, customersex, number, "", "",cus);
            if (company != "")
            {
                sql = string.Format(@"update customerTable set customerName='{0}',customersex='{1}',customernumber={2},
              cAddress='{3}',workAddress='{4}'where customerID={5}", customerName, customersex, number, company, "", cus);
            }
            if (Address != "")
            {
                sql = string.Format(@"update customerTable set customerName='{0}',customersex='{1}',customernumber={2},
              cAddress='{3}',workAddress='{4}'where customerID={5}", customerName, customersex, number, company, Address, cus);
            }
            DBHelper db = new DBHelper();
            bool result = db.ExcuteSql(sql);
            if (result == true)
            {
                MessageBox.Show("修改成功");
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }

        private void Updatecustomer_Load(object sender, EventArgs e)
        {
            string sql = string.Format("select * from customerTable where customerID={0}", cus);
            DBHelper dbr = new DBHelper();
            DataTable dta = dbr.GetDataTableBySql(sql);
            if (dta != null && dta.Rows.Count > 0)
            {
                this.NameBox.Text = dta.Rows[0]["customerName"].ToString();
                if (dta.Rows[0]["customersex"].ToString() == "男")
                {
                    this.ManRbt.Checked = true;
                }
                if (dta.Rows[0]["customersex"].ToString() == "女")
                {
                    this.WmanRbt.Checked = true;
                }
                this.NumberBox.Text= dta.Rows[0]["customernumber"].ToString();
                this.CompanyBox.Text = dta.Rows[0]["cAddress"].ToString();
                this.AddressBox.Text = dta.Rows[0]["workAddress"].ToString();
            }
           
        }
    }
}
